package com.archgeek.rpc.thrfit;

import com.archgeek.rpc.thrfit.gen.StudentService;

/**
 * @author pizhihui
 * @date 2022-04-06 21:03
 */
public class ThriftClientPool {
    static SocketPool pool = new SocketPool("127.0.0.1", 8732);

    private static volatile boolean flag = false; // 用于保证有一个线程已经开启

    public static void main(String[] args) throws Exception {

//        SocketPool pool = new SocketPool("127.0.0.1", 8732);
//
//        Runnable r = () -> {
//            try {
//                StudentService.Client client = pool.getClicent();
//
//                String s = client.sayHello("123");
//                System.out.println(s);
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        };
//        TlUtil.timeTasks(100, 1, r);

        new Job().start();

//        int jobCount = 100;
//        for (int i = 0; i < jobCount; i++) {
//            new Job().start();
//        }

//        while (true) {
//            boolean tem = flag; // 重要，跳出循环不能直接使用flag;
//            int active = pool.getNumActiveClient();
//            int idle = pool.getNumIdleClient();
//            int maxPoolSize = pool.getMaxPoolSize();
//            System.out.println("active=" + active + ", idle=" + idle + ", maxTotal=" + maxPoolSize);
//            if (tem && active == 0 && idle == pool.getMinIdle()) {
//                break;
//            }
//            Thread.sleep(1000);
//        }

    }


    static class Job extends Thread {

        @Override
        public void run() {
            try {
                StudentService.Client client = pool.getClicent();

                System.out.println("active=" + pool.getNumActiveClient() + ", idle=" + pool.getNumIdleClient() + ", maxTotal=" + pool.getMaxPoolSize());

                String s = client.sayHello("123");
                System.out.println(s);

                pool.releaseClient(client);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
